﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using ZGRemote.Common.Networking;
using ZGRemote.Common.Processor;
using ZGRemote.Server.Core.Models;
using ZGRemote.Common.Message.RemoteFileExplorer;
using System.Threading.Tasks;

namespace ZGRemote.Server.Core.Handler
{
    public class RemoteFileExplorerHandler : HandlerBase<RemoteFileExplorerHandler>
    {
        //public static FileModel[] GetFiles(UserContext user, string Path)
        //{
            
        //}

        public static async Task<string[]> GetDrivesAsync(UserContext user)
        {
            GetDrives message = new GetDrives();
            GetDrivesResponse response = await SendMessageAsync<GetDrivesResponse>(user, message);
            if (response.Status == false) throw new Exception(response.Message);
            return response.Names;
        }

        public static string[] GetDrives(UserContext user)
        {
            Console.WriteLine(Properties.Resources.Remote);
            GetDrives message = new GetDrives();
            GetDrivesResponse response = SendMessage<GetDrivesResponse>(user, message);
            if (response.Status == false) throw new Exception(response.Message);
            return response.Names;
        }

        public static async Task<FileModel[]> GetFilesAsync(UserContext user, string path)
        {
            var message = new GetFiles();
            message.Path = path;
            var response = await SendMessageAsync<GetFilesResponse>(user, message);
            if(response.Status == false) throw new Exception(response.Message);

            int size = response.FileName == null ? 0 : response.FileName.Length;
            FileModel[] files = new FileModel[size];
            for (int i = 0; i < size; i++)
            {
                files[i] = new FileModel();
                files[i].Name = response.FileName[i];
                files[i].LastWriteTime = response.LastWriteTime[i];
                files[i].Size = response.Size[i];
                if (response.FileType[i] == 0)
                {
                    files[i].Type = FileType.File;
                    
                }
                else
                {
                    files[i].Type = FileType.Folder;
                }
            }
            return files;
        }
    }
}
